/* Copyright (c) 2006 by M Aamir Maniar 
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the 
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * */

namespace Generic.DataStructures
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
	using System.Runtime.InteropServices;
    using Generic.Utils;

    public class SynchronizedQueue<T> : IQueue<T>
    {
        IQueue<T> _internalQueue;
        object _syncroot;

        public SynchronizedQueue(IQueue<T> queue)
        {
            ArgumentValidator.CheckIfNull<IQueue<T>>(queue);
            _internalQueue = queue;
            _syncroot = queue.SyncRoot;
        }

        #region IQueue<T> Members

        public void Enqueue(T item)
        {
            lock (_syncroot)
            {
                _internalQueue.Enqueue(item);
            }
        }

        public T Dequeue()
        {
            lock (_syncroot)
            {
                return _internalQueue.Dequeue();
            }
        }

        public bool FixedSize
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.FixedSize;
                }
            }
            set
            {
                lock (_syncroot)
                {
                    _internalQueue.FixedSize = value;
                }
            }
        }

        public bool IsFull
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.IsFull;
                }
            }
        }

        public bool IsEmpty
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.IsEmpty;
                }
            }
        }

        public bool IsSynchronized
        {
            get
            {
                lock (_syncroot)
                    return true;
            }
        }

        public object SyncRoot
        {
            get
            {
                return _internalQueue.SyncRoot;
            }
        }

        #endregion

        #region ICollection<T> Members

        public void Add(T item)
        {
            lock (_syncroot)
            {
                _internalQueue.Add(item);
            }
        }

        public void Clear()
        {
            lock (_syncroot)
            {
                _internalQueue.Clear();
            }
        }

        public bool Contains(T item)
        {
            lock (_syncroot)
            {
                return _internalQueue.Contains(item);
            }
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            lock (_syncroot)
            {
                _internalQueue.CopyTo(array, arrayIndex);
            }
        }

        public int Count
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.Count;
                }
            }
        }

        public bool IsReadOnly
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.IsReadOnly;
                }
            }
        }

        public bool Remove(T item)
        {
            lock (_syncroot)
            {
                return _internalQueue.Remove(item);
            }
        }

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            lock (_syncroot)
            {
                return _internalQueue.GetEnumerator();
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_internalQueue.GetEnumerator();
        }

        #endregion

        #region ICloneable Members

        public object Clone()
        {
            lock (_syncroot)
            {
                return _internalQueue.Clone();
            }
        }

        #endregion

        #region IQueue<T> Members


        public T First
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.First;
                }
            }
        }

        public T Last
        {
            get
            {
                lock (_syncroot)
                {
                    return _internalQueue.Last;
                }
            }
        }

        #endregion

        #region internal class LinkedListDebugView
        internal class QueueDebugView
        {
            Queue<T> _queue;
            public QueueDebugView(Queue<T> queue)
            {
                _queue = queue;
            }

            [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
            public T[] Items
            {
                get
                {
                    return _queue.ToArray();
                }
            }


        }
        #endregion
    }
}
